package com.iotechn.unimall.admin.api.money;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.ShopMoneyDO;
import com.iotechn.unimall.data.mapper.ShopMoneyMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminShopMoneyServiceImpl implements AdminShopMoneyService {
    @Autowired
    private ShopMoneyMapper shopMoneyMapper;

 

    @Override
    public Page<ShopMoneyDO> list(Long  shopId,  String title,Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopMoneyDO> wrapper = new EntityWrapper<ShopMoneyDO>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("company_name",title);
        }
        wrapper.orderBy("id", false);
        Integer count = shopMoneyMapper.selectCount(wrapper);
        List<ShopMoneyDO> ShopMoneyDOS = shopMoneyMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(ShopMoneyDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopMoneyDO create(ShopMoneyDO ShopMoneyDO, Long adminId) throws ServiceException {
        ShopMoneyDO.setAdminId(adminId);
        Date now = new Date();
        ShopMoneyDO.setGmtCreate(now);
        ShopMoneyDO.setGmtUpdate(now);
        if(ShopMoneyDO.getFileList()!=null){
            Gson gson=new Gson();
            ShopMoneyDO.setFile(gson.toJson(ShopMoneyDO.getFileList()));
        }
        if (shopMoneyMapper.insert(ShopMoneyDO) > 0) {
            ShopMoneyDO.setId(ShopMoneyDO.getId());
            return ShopMoneyDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopMoneyDO ShopMoneyDO, Long adminId) throws ServiceException {
        Long id = ShopMoneyDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        if(ShopMoneyDO.getFileList()!=null && ShopMoneyDO.getFileList().size()>0){
            Gson gson=new Gson();
            ShopMoneyDO.setFile(gson.toJson(ShopMoneyDO.getFileList()));
        }
        ShopMoneyDO.setGmtUpdate(now);
        if (shopMoneyMapper.updateById(ShopMoneyDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopMoneyMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopMoneyMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopMoneyDO detail(Long id, Long adminId) throws ServiceException {
        return shopMoneyMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopMoneyDO ShopMoneyDO = new ShopMoneyDO();
        ShopMoneyDO.setId(id);
        ShopMoneyDO.setGmtUpdate(new Date());
        if (shopMoneyMapper.updateById(ShopMoneyDO) > 0) {
            return true;
        }
        return false;
    }

}